#include "ccms/nr/node_stats.h"
#include "arlib/exception/errno_exception.h"
#include <cstdio>
#include <cstdlib>

using namespace ccms::nr;

int main(int argc, char* argv[])
{
    NodeStats nodeStats;
    int N = 10;

    try {
        while(N--) {
            nodeStats.update();
            printf("swap total: %lukB, swap free: %lukB, swap used: %lukB\n",
                nodeStats.getTotalMemUsage(MEM_USAGE_MODE_SWAP_TOTAL),
                nodeStats.getTotalMemUsage(MEM_USAGE_MODE_SWAP_FREE),
                nodeStats.getTotalMemUsage(MEM_USAGE_MODE_SWAP_USED));
            printf("user: %d%%, nice: %d%%, system: %d%%, idle: %d%%, iowait: %d%%, irq: %d%%, softirq: %d%%, steal: %d%%, guest: %d%%\n",
                nodeStats.getTotalCpuUsage(CPU_USAGE_MODE_USER),
                nodeStats.getTotalCpuUsage(CPU_USAGE_MODE_NICE),
                nodeStats.getTotalCpuUsage(CPU_USAGE_MODE_SYSTEM),
                nodeStats.getTotalCpuUsage(CPU_USAGE_MODE_IDLE),
                nodeStats.getTotalCpuUsage(CPU_USAGE_MODE_IOWAIT),
                nodeStats.getTotalCpuUsage(CPU_USAGE_MODE_IRQ),
                nodeStats.getTotalCpuUsage(CPU_USAGE_MODE_SOFTIRQ),
                nodeStats.getTotalCpuUsage(CPU_USAGE_MODE_STEAL),
                nodeStats.getTotalCpuUsage(CPU_USAGE_MODE_GUEST));
            printf("physTotal: %lukB, physFree: %lukB, physBuffers: %lukB, physCached: %lukB, physUsed: %lukB, swapTotal: %lukB, swapFree: %lukB, swapUsed: %lukB\n",
                nodeStats.getTotalMemUsage(MEM_USAGE_MODE_PHYS_TOTAL),
                nodeStats.getTotalMemUsage(MEM_USAGE_MODE_PHYS_FREE),
                nodeStats.getTotalMemUsage(MEM_USAGE_MODE_PHYS_BUFFERS),
                nodeStats.getTotalMemUsage(MEM_USAGE_MODE_PHYS_CACHED),
                nodeStats.getTotalMemUsage(MEM_USAGE_MODE_PHYS_USED),
                nodeStats.getTotalMemUsage(MEM_USAGE_MODE_SWAP_TOTAL),
                nodeStats.getTotalMemUsage(MEM_USAGE_MODE_SWAP_FREE),
                nodeStats.getTotalMemUsage(MEM_USAGE_MODE_SWAP_USED));
        }
    }
    catch(arlib::exception::ErrnoException& e) {
        printf("Exception in file: %s, line: %d\n", e.getExceptionFile(), e.getExceptionLine());
        printf("Exception reason: %s\n", e.what());
        printf("Exception errnum: %d\n", e.getErrnum());
        return 1;
    }
    catch(arlib::exception::Exception& e) {
        printf("Exception in file: %s, line: %d\n", e.getExceptionFile(), e.getExceptionLine());
        printf("Exception reason: %s\n", e.what());
        return 1;
    }
    return 0;
}
